#include "GamePlatformW32.h"

// SDL Headers
#include "sdl/SDL.h"

// OpenGL Headers
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>

// Link to OpenGL Win32 libs
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")

// Project headers
#include "../Defines.h"
#include "../GameCommon.h"
//#include "../Log.h"
//#include "../System/touchInput.h"
//#include "../System/memory.h"

#define IPHONE_SIMULATOR_WINDOW_TITLE	"iPhone Simulator"

// ------------------------------------------------------------------
bool _gameWindowAlive = true;
bool _mouseLeftButtonDown = false;
bool _keyShiftHold = false;
#include "Game.h"
extern CGame* gp_GameApp;
// ------------------------------------------------------------------
int GameWindowCreate()
{
	const SDL_VideoInfo* info = NULL;
    int width = SCREEN_WIDTH;
	int height = SCREEN_HEIGHT;
    int bpp = 0;

    // First, initialize SDL's video subsystem.
    if(SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		SDL_GetError();
		GameWindowDestroy();
		return -1;
    }

    // Get video information.
    info = SDL_GetVideoInfo( );

    if(!info)
	{
		SDL_GetError();
		GameWindowDestroy();
        return -1;
    }

	// Set the bpp info
    bpp = info->vfmt->BitsPerPixel;

	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);

	SDL_WM_SetCaption(IPHONE_SIMULATOR_WINDOW_TITLE, NULL);

	if (SDL_SetVideoMode( width, height, bpp, SDL_OPENGL) == 0)
	{
        SDL_GetError();
		GameWindowDestroy();
		return -1;
    }
	
	_gameWindowAlive = true;

	// Call the common game start
	nCGame::gamePreStart();
	nCGame::gameStart();

	return 0;
}

// ------------------------------------------------------------------
void GameWindowDestroy()
{
	_gameWindowAlive = false;
	
	// Call the common game stop
	nCGame::gameStop();
	nCGame::gameRelease();

	// Close the log file
	//logToFileClose();

#ifdef DEBUG_MEM_LEACK
	DumpUnfreed();
#endif


	SDL_Quit();
}

// ------------------------------------------------------------------
bool GameWindowIsAlive()
{
	return _gameWindowAlive;
}
// ------------------------------------------------------------------
void GameWindowProcEvents()
{
    SDL_Event event;

    // Take events from the queue
    while(SDL_PollEvent(&event))
	{

        switch( event.type )
		{
        case SDL_KEYDOWN:
#ifdef DEBUG_MEM_LEACK
			//Press P to show current memory usage
			if (event.key.keysym.sym == SDLK_p)
			{
				DumpUnfreed();
			}
#endif
			if (event.key.keysym.sym == SDLK_r)
			{
				nCGame::_gameRestart = true;
			}
			else if (event.key.keysym.sym == SDLK_p)
			{
				nCGame::_gamePaused = !nCGame::_gamePaused;
			}
			else if (event.key.keysym.sym == SDLK_s)
			{
				//nCGame::_gameNeedSave = true;
			}
			else if (event.key.keysym.sym == SDLK_F1)
			{
				if(_keyShiftHold)
				{
					gp_GameApp->SaveData();
				}
				else
				{
					gp_GameApp->LoadData();
				}
			}
			/*else if (event.key.keysym.sym == SDLK_F2)
			{
				if(_keyShiftHold)
				{
					gp_GameApp->Save(1);
				}
				else
				{
					gp_GameApp->Load(1);
				}
			}
			else if (event.key.keysym.sym == SDLK_F3)
			{
				if(_keyShiftHold)
				{
					gp_GameApp->Save(2);
				}
				else
				{
					gp_GameApp->Load(2);
				}
			}*/
			else if (event.key.keysym.sym == SDLK_LSHIFT)
			{
				_keyShiftHold = true;
			}
            break;
		case SDL_KEYUP:
			_keyShiftHold = false;
			break;
        case SDL_QUIT:
            GameWindowDestroy();
            break;

		case SDL_MOUSEBUTTONDOWN:
			nCGame::gameMouseDown(event.button.x, event.button.y);
			_mouseLeftButtonDown = true;
			break;
		case SDL_MOUSEBUTTONUP:
			nCGame::gameMouseUp(event.button.x, event.button.y);
			_mouseLeftButtonDown = false;
			break;

		case SDL_MOUSEMOTION:
			if (_mouseLeftButtonDown)
			{
				nCGame::gameMouseDrag(event.motion.x, event.motion.y);
			}

			break;
		}
    }
}

// ------------------------------------------------------------------
void GameWindowPaint()
{
	if (!_gameWindowAlive) return;

	// Common code of update
	if(nCGame::gameUpdate())
	{
		// Common code of drawing
		nCGame::gamePaint();
		
		// Swap buffers
		TIME_DEBUG_TIME_CHECK("PSwapB");
		SDL_GL_SwapBuffers();
		TIME_DEBUG_TIME_CHECK("SwapB");
	}
}
